package com.kkd.kkdapp.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.KKDWebActivity;
import com.kkd.kkdapp.activity.LoginActivity;
import com.kkd.kkdapp.http.entity.FindDataActiveItem;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.view.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class FindMoreAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater inflater;
	private List<FindDataActiveItem> list;

	public FindMoreAdapter(Context context, List<FindDataActiveItem> list) {
		mContext = context;
		inflater = LayoutInflater.from(mContext);
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_active_list, parent,
					false);
			holder.view_top = convertView.findViewById(R.id.view_top);
			holder.linear_content = (LinearLayout) convertView
					.findViewById(R.id.linear_content);
			holder.iv_content = (CircleImageView) convertView
					.findViewById(R.id.iv_content);
			holder.linear_flag_outTime = (LinearLayout) convertView
					.findViewById(R.id.linear_flag_outTime);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.iv_content.setCircleRadiu(SystemUtils.dip2px(mContext, 6));
		if (position == 0) {
			holder.view_top.setVisibility(View.VISIBLE);
		} else {
			holder.view_top.setVisibility(View.GONE);
		}
		ImageLoader.getInstance().displayImage(list.get(position).getImage(),
				holder.iv_content, R.drawable.home_ad);
		if (list.get(position).getStatus() == 2) { // 活动已结束
			holder.linear_flag_outTime.setVisibility(View.VISIBLE);
		} else {
			holder.linear_flag_outTime.setVisibility(View.GONE);
		}
		holder.linear_content.setTag(position);
		holder.linear_content.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if (!TextUtils.isEmpty(list.get((Integer) v.getTag()).getUrl())) {
					Intent intent = new Intent(mContext, KKDWebActivity.class);
					intent.putExtra("titleName", list.get((Integer) v.getTag())
							.getTitle());
					intent.putExtra("desc", list.get((Integer) v.getTag())
							.getDesc());
					intent.putExtra("useActiveTitle", true);
					intent.putExtra("shareUrl", list.get((Integer) v.getTag())
							.getShareUrl());
					intent.putExtra("url", list.get((Integer) v.getTag())
							.getUrl());
					mContext.startActivity(intent);
				}
			}
		});
		return convertView;
	}

	class ViewHolder {
		CircleImageView iv_content;
		LinearLayout linear_content;
		View view_top;
		LinearLayout linear_flag_outTime;
	}
}
